import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import ElementPlus from "unplugin-element-plus/vite";
import path from "path";
// https://vite.dev/config/

export default defineConfig({
  // ...
  plugins: [
    vue(),
    // ...
    AutoImport({
      resolvers: [ElementPlusResolver({
        importStyle: "sass",
      })],
    }),
    Components({
      resolvers: [ElementPlusResolver({
        importStyle: "sass",
      })],
    }),
    ElementPlus({
      useSource: true,
      defaultLocale: "zh-tw",
    }),
  ],
  resolve: {
    alias: {
      "~/": `${path.resolve(__dirname, "src")}/`,
    },
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "~/theme/style.scss" as *;`,
      },
    },
  },
  server: {  
    proxy: {  //代理配置
      
      // 第一个代理
      '/fnji': {
        target: 'https://website.fnji.com',
        changeOrigin: true,
        configure: (proxy, options) => {
          proxy.on("proxyReq", (proxyReq, req, res) => {
            // 在代理请求中添加自定义 header ，伪装本次请求
            // host:website.fnji.com
            // origin:https://www.fnji.com
            // referer:https://www.fnji.com/
            proxyReq.setHeader("host", "website.fnji.com"); //从目标网站找到请求头
            proxyReq.setHeader("origin", "https://www.fnji.com");
            proxyReq.setHeader("Referer", "https://www.fnji.com/");
          });
          
        },
        rewrite: (path) => path.replace(/^\/fnji/, ''),
      },
      
      // 第二个代理【按需加多个代理】
      '/tyapi': {
        target: 'https://tyapi.71360.com',
        changeOrigin: true,
        configure: (proxy, options) => {
          proxy.on("proxyReq", (proxyReq, req, res) => {
            // 在代理请求中添加自定义 header ，伪装本次请求
            // host:tyapi.71360.com
            // origin:http://www.ccsinter.com
            // referer:http://www.ccsinter.com/
            proxyReq.setHeader("host", "tyapi.71360.com"); //从目标网站找到请求头
            proxyReq.setHeader("origin", "http://www.ccsinter.com");
            proxyReq.setHeader("Referer", "http://www.ccsinter.com/");
          });
        },
        rewrite: (path) => path.replace(/^\/tyapi/, ''),
      },
    },
  },
})
